<?php
// Blogmer Core Installation
include('codeigniter_library\sha1.php');
//automatic install
if($_POST['action']=="step_one")
{
	
	//server details
	$server_name=$_POST['server_name'];
	$user_name=$_POST['user_name'];
	$user_pass=$_POST['user_password'];
	$database_name=$_POST['database_name'];
	
	//blog details
	$blog_name=$_POST['blog_name'];
	$blog_title=$_POST['blog_title'];
	$blog_owner=$_POST['blog_owner'];
	$blog_url=$_POST['blog_url'];
	
	//admin account
	$admin_name=$_POST['admin_name'];
	$admin_password=$_POST['admin_password'];
	$admin_email=$_POST['admin_email'];
	
	
	if(empty($server_name)||empty($user_name)||empty($database_name)||empty($user_pass)||empty($blog_name)||empty($blog_title)||empty($blog_owner)||empty($blog_url)||empty($admin_name)||empty($admin_password))
		die('<div id="error"><img src="img/error.png" />&nbsp;&nbsp;Fields are empty. All fields are required.</div>');


	 $link = @mysql_connect($server_name, $user_name, $user_pass) or die('<div id="error"><img src="img/error.png" />&nbsp;&nbsp;Could not connect server </div>');
	 @mysql_select_db($database_name) or die('<div id="error"><img src="img/error.png" />&nbsp;&nbsp;Could not select database</div>');
	 mysql_close($link);
	
	 echo '<div id="imp"><img src="img/tick.png" />&nbsp;&nbsp;successfully connected</div>';
	
	//We got now execute database sql file to install database
	ExecuteSql($server_name,$user_name,$user_pass,$database_name);
	
	mysql_query("INSERT INTO generalsettings VALUES
(1,'".$blog_name."' , '".$blog_title."', '".$blog_owner."', '1', '0', '1', '1', '1', '0', '0', '0', '0', '0', '0', '0', '1', '0', '1', '1', '1', 'Vector', 'vector');
");
	
	$pass=generate($admin_password);
	
	$date=date("Y-m-d H:i:s");
	
	mysql_query("INSERT INTO users  VALUES (1, '".$admin_name."', '".$pass."', 'http://killer.com', '".$admin_email."', '', '', 'a72b20062ec2c47ab2ceb97ac1bee818f8b6c6cb', '".$date."', '1', '')");
	
	$destination_database = "../../system/application/config/database.php";
	$destination_config = "../../system/application/config/config.php";
	
	
	//server details
	$arr['@hostname@']=$server_name;
	$arr['@username@']=$user_name;
	$arr['@password@']=$user_pass;
	$arr['@database@']=$database_name;
	MoveFile("database.php",$destination_database,$arr);
	
	//config details
	$arr['#blogurl#']=$blog_url;
	MoveFile("config.php",$destination_config,$arr);

	echo '<div id="imp">
			<img src="img/tick.png" />
					&nbsp;&nbsp;Blogmer is successfully installed.
		  </div>'."\n";

	
}

//manual install
if($_POST['action']=="manual_install")
{
	ManualInstall();
}	





function MoveFile($filename,$destination,$arr)
{

	copy($filename,"database_backup.php");
	$content=file_get_contents($filename); 	
	
		
	foreach ($arr as $key => $value)
  		$content=str_replace($key,$value,$content);

	
	// Let's make sure the file exists and is writable first.
	if (is_writable($filename)) 
	{
	
		// In our example we're opening $filename in write mode.
		// The file pointer is at the bottom of the file hence
		// that's where $somecontent will go when we fwrite() it.
		if (!$handle = fopen($filename, 'w+')) 
		{
			 echo "Cannot open file ($filename)";
			 exit;
		}
	
		// Write $somecontent to our opened file.
		if (fwrite($handle, $content) === FALSE) 
		{
			echo "Cannot write to file ($filename)";
			exit;
		}
	
		//echo "Success, wrote ($content) to file ($filename)";
		fclose($handle);
		unlink($destination);
                 
   		if (rename($filename, $destination)) 
		{
      		echo '<div id="imp"><img src="img/tick.png" />&nbsp;&nbsp;The '.$filename.' created successfully.</div>'."\n";
			rename("database_backup.php",$filename);
   		} 
		else 
		{
      		echo '<div id="error">The specified file could not be moved. Please try again.</div>'."\n";
   		}
		
			} 
	else 
	{
		echo "<h3>Manual Installation</h3>The file $filename is not writable";
		
		echo "<br />Copy all text and paste into a file.<br />Name it as database.php and copy it to system/application/config/";
		echo "<br />You are done<br />";
		$content=@file_get_contents('backup_database.php')."".$content;
		echo '<textarea name="noname" cols="60" rows="10" readonly="readonly">'.$content.'</textarea>';
	}
}	



function ManualInstall()
{
	$server_name=$_POST['server_name'];
	$user_name=$_POST['user_name'];
	$user_pass=$_POST['user_password'];
	$database_name=$_POST['database_name'];
	
	if(empty($server_name)||empty($user_name)||empty($database_name)||empty($user_pass))
		die('<div id="error"><img src="img/error.png" />&nbsp;&nbsp;Fields are empty. All fields are required.Fill above fields.such server name,user name etc...</div>');
	
	$content=file_get_contents('database.php'); 	
	$content=str_replace("@username@",$username,$content);
	$content=str_replace("@password@",$password,$content);
	$content=str_replace("@hostname@",$servername,$content);
	$content=str_replace("@database@",$database,$content);
	
	echo "<h3>Manual Installation</h3>";
	echo "<br />Copy all text and paste into a file.<br />Name it as <strong>database.php</strong> and copy it to system/application/config/";
	echo "<br />You are done<br />";
	echo '<textarea name="noname" cols="80" rows="19" readonly="readonly">'.$content.'</textarea>';
	

	
}

/*
* Restore MySQL dump using PHP
* (c) 2006 Daniel15
* Last Update: 9th December 2006
* Version: 0.1
*
* Please feel free to use any part of this, but please give me some credit :-)
*/	
function ExecuteSql($mysql_host,$mysql_username,$mysql_password,$mysql_database)
{

	$filename="blog.sql";
	// Connect to MySQL server
	mysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to MySQL server: ' . mysql_error());
	// Select database
	mysql_select_db($mysql_database) or die('Error selecting MySQL database: ' . mysql_error());
 
 	// Temporary variable, used to store current query
	$templine = '';
	// Read in entire file
	$lines = file($filename);
	// Loop through each line
	foreach ($lines as $line_num => $line) 
	{
  		// Only continue if it's not a comment
  		if (substr($line, 0, 2) != '--' && $line != '') 
		{
    		// Add this line to the current segment
    		$templine .= $line;
    		// If it has a semicolon at the end, it's the end of the query
    		if (substr(trim($line), -1, 1) == ';') 
			{
      			// Perform the query
      			mysql_query($templine) or 
				print('Error performing query \'<b>' . substr($templine,0,30) . '...</b>\': ' 
				.mysql_error() . '<br /><br />');
				
				// Reset temp variable to empty
     			$templine = '';
			}
		} // end of if
	}//end of foreach

	//echo '<div id="imp"><img src="img/tick.png" />&nbsp;&nbsp;Tables are installed successfully</div>';
	
}//end of function	

?>
